import threading

import pymysql
import requests
from bs4 import BeautifulSoup


def readData(db, redballnum):
	try:
		sql = 'SELECT %s FROM %s' % (redballnum, db)
		conn = pymysql.connect(host='localhost', user='root', passwd="HxP1232061", db="lottery", port=3306,
		                       charset='utf8')
		cur = conn.cursor()
		cur.execute(sql)
		redball1list = []
		for i in cur.fetchall():
			redball1list.append(i[0])
		length = cur.rowcount
		redball1list.sort(reverse=True)
		return redball1list[0]
		conn.commit()
		cur.close()
		conn.close()
	except Exception as e:
		with open('db_fail.txt', 'a')as f:
			f.write('%s    %s\n' % (db, e))


def getNumber():
	header = {
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'}
	main_url = "http://kaijiang.500.com/shtml/ssq/19151.shtml"

	r = requests.get(main_url, header)
	r.encoding = r.apparent_encoding
	soup = BeautifulSoup(r.content, "html.parser")
	number = []
	dataMaxNumber = readData('doublecolordata', 'number')
	for a in soup.find('div', attrs={'class': 'iSelectList'}).children:
		try:
			if int(a.string) > int(dataMaxNumber):
				number.append(a.get_text())
		except:
			continue
	return number


def get_info():
	number = getNumber()
	for i in number:
		header = {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'}
		data = []
		data.append(i)
		url = "http://kaijiang.500.com/shtml/ssq/%s.shtml" % i
		# print(url)
		r = requests.get(url, header)
		r.encoding = r.apparent_encoding
		soup = BeautifulSoup(r.text, 'html.parser')
		balls = soup.find('table', attrs={'cellpadding': '1'})
		red_ball = balls('td')[-1].string.strip().split(' ')
		for i in red_ball:
			data.append(i)
		blue_ball = soup.find('li', attrs={'class': "ball_blue"}).string
		data.append(blue_ball)
		# print(data)
		insert('doublecolordata', data)


def insert(db, values):
	try:
		sql = 'insert into %s values (%s)' % (db, ','.join("'%s'" % x for x in values))
		print(sql)
		conn = pymysql.connect(host='localhost', user='root', passwd="HxP1232061", db="lottery", port=3306,
		                       charset='utf8')
		cur = conn.cursor()
		print("connect ok")
		cur.execute(sql)
		print("数据插入完毕")
		conn.commit()
		cur.close()
		conn.close()
	except Exception as e:
		with open('db_fail.txt', 'a')as f:
			f.write('%s    %s\n' % (db, e))


if __name__ == '__main__':
	p = threading.Thread(target=get_info)
	p.start()
	p.join()
